﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using KKSGameLibrary.Logic;
using System.Windows.Media.Imaging;
using System.Windows.Media.Effects;
using KaKaSongGame.CtrlEx;

namespace KaKaSongGame
{
    public partial class GameCard : UserControl
    {

        public GameCard()
        {
            InitializeComponent();
        }

        public void Highlight(int secs)
        {
            highlightsb.Begin();
            MyTimer.DelayExcute(TimeSpan.FromSeconds(5), delegate
            {
                highlightsb.Stop();
            });
        }

        public CardInMap CardData
        {
            get { return (CardInMap)GetValue(CardDataProperty); }
            set { SetValue(CardDataProperty, value); }
        }

        public static readonly DependencyProperty CardDataProperty =
            DependencyProperty.Register("CardData", typeof(CardInMap), typeof(GameCard),
            new PropertyMetadata(
                new PropertyChangedCallback((s, e) =>
                {
                    var gc = s as GameCard;
                    var card = e.NewValue as CardInMap;
                    if (card != null)
                        gc.InitView(card);
                })));

        void InitView(CardInMap cardim)
        {
            string cardimgpath =
                "/KaKaSongGame.Client;component/CardImages/"
                + Byte4ToString(cardim.Card.CastleData)
                + "_"
                + Byte4ToString(cardim.Card.RoadData)
                + ".jpg";
            img1.Source = new BitmapImage(
                new Uri(cardimgpath, UriKind.Relative));

            ((RotateTransform)img1.RenderTransform).Angle = AngleTransform(cardim.Rotation);

            //CardData = cardim;
            CanRotate = false;
        }

        string Byte4ToString(int intvalue)
        {
            var strvalue = intvalue.ToString("X").ToLower();
            for (int i = strvalue.Length; i < 8; i++)
                strvalue = strvalue.Insert(0, "0");
            return "0x" + strvalue;
        }

        int AngleTransform(RotationDegree d)
        {
            int ret = 0;
            switch (d)
            {
                case RotationDegree.D0:
                    ret = 0;
                    break;
                case RotationDegree.D90:
                    ret = 90;
                    break;
                case RotationDegree.D180:
                    ret = 180;
                    break;
                case RotationDegree.D270:
                    ret = 270;
                    break;
                default:
                    break;
            }
            return ret;
        }

        Point pos;
        private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pos = e.GetPosition(this.Parent as FrameworkElement);
            ((FrameworkElement)sender).CaptureMouse();
        }

        private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var curpos = e.GetPosition(this.Parent as FrameworkElement);
            if (pos.X == curpos.X && pos.Y == curpos.Y)
            {
                Rotate();
            }
            ((FrameworkElement)sender).ReleaseMouseCapture();
        }

        public bool CanRotate { set; get; }
        private void Rotate()
        {
            if (CanRotate)
            {
                CardData.Rotation = (RotationDegree)(((int)CardData.Rotation + 1) % 4);
                //((DoubleAnimation)clockwiseSB.Children[0]).By = 90;
                clockwiseSB.Begin();
            }
        }
    }
}
